//
//  WLScoreRankingSubController.m
//  WanLian
//
//  Created by muzhuang on 2018/10/18.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLScoreRankingSubController.h"
#import "WLScoreRankCell.h"

@interface WLScoreRankingSubController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray    *arrRanks;
@property (nonatomic, strong) UITableView   *tbRank;
@property (nonatomic, assign) NSInteger     pageRank;

@end

@implementation WLScoreRankingSubController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"积分排行榜";
    
    _arrRanks = [[NSMutableArray alloc] init];
    _pageRank = 1;
    
    _tbRank = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tbRank.backgroundColor = wlUIColorRGB(0xFAFAFA);
    _tbRank.delegate = self;
    _tbRank.dataSource = self;
    _tbRank.rowHeight = WLScaleX(50);
    if (@available(iOS 11.0, *)) {
        _tbRank.estimatedSectionHeaderHeight = 0;
        _tbRank.estimatedSectionFooterHeight = 0;
    }
    _tbRank.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tbRank];
    WLWkSelf
    [_tbRank mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    
    _tbRank.mj_header = [WLFreshGifHeader headerWithRefreshingBlock:^{
        weakSelf.tbRank.mj_footer = nil;
        weakSelf.pageRank = 1;
//        [weakSelf reqRankData];
        [weakSelf totalRankList];
    }];
    
//    [self reqRankData];
    [self totalRankList];
}

- (void)addRankFooterRefresh
{
    WLWkSelf
    _tbRank.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        [weakSelf reqRankData];
        [weakSelf totalRankList];
    }];
}

- (void)totalRankList
{
    NSDictionary *conditions = @{};
//    NSDictionary *pages = @{@"page":@(_pageRank),@"num":@10};
    NSDictionary *pages = @{@"page":@(1),@"num":@10};
    NSDictionary *paramsp = @{@"conditions":conditions,@"page":pages};
    WLWkSelf
    [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"score/app/sumscore/order" withParamsDic:paramsp success:^(NSDictionary *successDict) {
        __strong __typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.tbRank.mj_header endRefreshing];
        strongSelf.arrRanks = successDict[@"object"];
//        NSInteger productCount = [successDict[@"count"] integerValue];
//        if (strongSelf.pageRank==1)
//        {
//            [strongSelf.arrRanks removeAllObjects];
//            if (productCount>10)
//            {
//                [strongSelf addRankFooterRefresh];
//            }
//        }
//        NSArray *arrPros = successDict[@"object"];
//        if (arrPros.count>0) {
//            [strongSelf.arrRanks addObjectsFromArray:arrPros];
//            if (strongSelf.arrRanks.count >= productCount) {
//                [strongSelf.tbRank.mj_footer endRefreshingWithNoMoreData];
//            }
//            else
//            {
//                [strongSelf.tbRank.mj_footer endRefreshing];
//            }
//        }
//        else
//        {
//            [strongSelf.tbRank.mj_footer endRefreshingWithNoMoreData];
//        }
        [strongSelf.tbRank reloadData];
//        strongSelf.pageRank += 1;
    } failure:^(NSString *errorMsg, NSInteger errorTag) {
        [weakSelf showHUDMessage:errorMsg];
        [weakSelf.tbRank.mj_header endRefreshing];
//        [weakSelf.tbRank.mj_footer endRefreshing];
    } error:^(NSError *error) {
        [weakSelf.tbRank.mj_header endRefreshing];
//        [weakSelf.tbRank.mj_footer endRefreshing];
    } inView:(self.pageRank==1&&_tbRank.mj_header.isRefreshing==NO)?self.view:nil];
}

- (void)reqRankData
{
    NSString *strType = @"";
    if (self.rankType==0) {
        strType = @"week";
    }
    else if (self.rankType==1)
    {
        strType = @"month";
    }
    
    NSDictionary *conditions = @{@"date":strType};
    NSDictionary *pages = @{@"page":@(_pageRank),@"num":@10};
    NSDictionary *paramsp = @{@"conditions":conditions,@"page":pages};
    WLWkSelf
    [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"score/app/score/order" withParamsDic:paramsp success:^(NSDictionary *successDict) {
        __strong __typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.tbRank.mj_header endRefreshing];
        NSInteger productCount = [successDict[@"count"] integerValue];
        if (strongSelf.pageRank==1)
        {
            [strongSelf.arrRanks removeAllObjects];
            if (productCount>10)
            {
                [strongSelf addRankFooterRefresh];
            }
        }
        NSArray *arrPros = successDict[@"object"];
        if (arrPros.count>0) {
            [strongSelf.arrRanks addObjectsFromArray:arrPros];
            if (strongSelf.arrRanks.count >= productCount) {
                [strongSelf.tbRank.mj_footer endRefreshingWithNoMoreData];
            }
            else
            {
                [strongSelf.tbRank.mj_footer endRefreshing];
            }
//            [strongSelf.tbRank reloadData];
        }
        else
        {
            [strongSelf.tbRank.mj_footer endRefreshingWithNoMoreData];
        }
        [strongSelf.tbRank reloadData];
        strongSelf.pageRank += 1;
    } failure:^(NSString *errorMsg, NSInteger errorTag) {
        [weakSelf showHUDMessage:errorMsg];
        [weakSelf.tbRank.mj_header endRefreshing];
        [weakSelf.tbRank.mj_footer endRefreshing];
    } error:^(NSError *error) {
        [weakSelf.tbRank.mj_header endRefreshing];
        [weakSelf.tbRank.mj_footer endRefreshing];
    } inView:(self.rankType==0&&self.pageRank==1&&_tbRank.mj_header.isRefreshing==NO)?self.view:nil];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrRanks.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    static NSString *strRank = @"rankCell";
    WLScoreRankCell *cell = [tableView dequeueReusableCellWithIdentifier:strRank];
    if (!cell) {
        cell = [[WLScoreRankCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strRank];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSDictionary *dictRank = self.arrRanks[row];
    if (row<3)
    {
        cell.ivRankBack.hidden = NO;
        if (row==0)
        {
            cell.ivRankBack.image = [UIImage imageNamed:@"rank_first"];
        }
        else if (row==1)
        {
            cell.ivRankBack.image = [UIImage imageNamed:@"rank_second"];
        }
        else if (row==2)
        {
            cell.ivRankBack.image = [UIImage imageNamed:@"rank_third"];
        }
        cell.labRank.font = [WLCommonTool fontTypePingFangSCRegular:10];
        cell.labRank.textColor = [UIColor whiteColor];
    }
    else
    {
        cell.ivRankBack.hidden = YES;
        cell.labRank.font = [WLCommonTool fontTypePingFangSCRegular:14];
        cell.labRank.textColor = wlDarkGrayColor;
    }
    cell.labRank.text = [NSString stringWithFormat:@"%ld",row+1];
    NSString *strImg = dictRank[@"headimg"];
    if (strImg==nil||[strImg isKindOfClass:[NSNull class]]) {
        cell.ivHead.image = nil;
    }
    else
    {
        [cell.ivHead sd_setImageWithURL:[NSURL URLWithString:strImg] placeholderImage:nil];
    }
    cell.labName.text = dictRank[@"username"];
    cell.labScore.text = [NSString stringWithFormat:@"%.6f",[dictRank[@"score"] floatValue]];
    
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
